package com.wgts.wgts.netmodle;

import com.wgts.wgts.BuildConfig;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

import java.util.Properties;

public class FileManager implements FileManagerConfig  {
    private static final long serialVersionUID = 1L;
    private static TrackerClient trackerClient;
    private static TrackerServer trackerServer;
    private static StorageServer storageServer;
    private static StorageClient storageClient;



    private static FileManager instance = null;

    public static FileManager getInstance(){
        if(instance == null){
            instance = new FileManager();
        }
        return instance;
    }

    private FileManager(){
        try {

            Properties props = new Properties();
            props.put(ClientGlobal.PROP_KEY_TRACKER_SERVERS, BuildConfig.FASTDFS);
            ClientGlobal.initByProperties(props);

            trackerClient = new TrackerClient();
            trackerServer = trackerClient.getConnection();

            storageClient = new StorageClient(trackerServer, storageServer);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public String upload(FastDFSFile file,NameValuePair[] valuePairs) {
        String[] uploadResults = null;
        try {
            uploadResults = storageClient.upload_file("group2", file.getContent(),file.getExt(), valuePairs);
            String groupName = uploadResults[0];
            String remoteFileName = uploadResults[1];

            String fileAbsolutePath = BuildConfig.FASTDFS_BASE + SEPARATOR + groupName
                    + SEPARATOR + remoteFileName;
            return fileAbsolutePath;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

    }






}
